package com.yjd.pub.funds.service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import com.yjd.comm.funds.model.FundsPfModel;
import com.yjd.comm.funds.service.IFundsPfService;
import com.yjd.comm.funds.service.IRechargeOrderService;
import com.yjd.comm.merchant.service.IMerchantCashService;
import com.yjd.comm.repayment.service.IRepaymentClearService;
import com.yjd.comm.repayment.service.IRepaymentDataService;
import com.yjd.comm.util.ArithUtil;
import com.yjd.comm.util.DateUtil;
import com.yjd.comm.util.FrameUtil;
import org.springframework.stereotype.Service;

import com.yjd.comm.base.dao.IBaseDao;
import com.yjd.comm.base.service.impl.BaseServiceImpl;
import com.yjd.pub.funds.dao.IFundsPfDao;

/**
 * <b>description</b>：平台资金记录表业务实现<br>
 * <b>time</b>：2017-11-15 17:03:45 <br>
 * <b>author</b>：  carl
 */
@Service("fundsPfService")
public class FundsPfServiceImpl extends BaseServiceImpl implements IFundsPfService {

	/**
	 * 插入
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public FundsPfModel insert(FundsPfModel model) throws Exception {
		return this.fundsPfDao.insertModel(model, true);
	}

	/**
	 * 更新
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public int update(FundsPfModel model) throws Exception {
		return this.fundsPfDao.updateModel(model, true);
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public int delete(long id) throws Exception {
		Map<Object, Object> paramMap = new HashMap<Object, Object>();
		paramMap.put("id", id);
		return this.fundsPfDao.deleteModel(paramMap, true);
	}

	@Override
	public void clearBalance(Long start_time, Long end_time,Double begin_balance) throws Exception {
		if(start_time==null||end_time==null){
			FrameUtil.throwBaseException1("清算资金开始日期不能为空！");
		}

		if(end_time==null){
			FrameUtil.throwBaseException1("清算资金结束日期不能为空！");
		}

		if (start_time.intValue()>end_time.intValue()) {
			FrameUtil.throwBaseException1("开始日期不能小于结束日期");
		}

		Long startDateTime = DateUtil.getFristSecond(start_time);
		Long endDateTime = DateUtil.getFristSecond(end_time);
        Long currtime = FrameUtil.getTime();
		Long currtDate = DateUtil.getCurrDateFristSecond();

		if (startDateTime.intValue() >= currtDate.intValue()) {
			FrameUtil.throwBaseException1("开始日期不能大于当前日期");
		}

		if (endDateTime.intValue() >= currtDate.intValue()) {
			FrameUtil.throwBaseException1("结束日期不能大于等于当前日期");
		}

		do {
			begin_balance = generatePfBalance(begin_balance,startDateTime, currtime);//生成平台余额日结
			startDateTime = startDateTime+86400;
			if (startDateTime > endDateTime) {
				break;
			}
		} while (true);
	}


	private Double generatePfBalance(Double currBalance,Long start_time,Long currtimes)throws Exception{
		Long endTime = start_time + 86399;
		Double balance = currBalance;
		Double pay_price = rechargeOrderService.querySumPayPrice(start_time,endTime);
		Double repayment_a = repaymentDataService.querySumPepaymentPrice(start_time,endTime);
		Double repayment_b = repaymentClearService.querySumRepaymentPrice(start_time,endTime);
		Double profit_b = repaymentClearService.querySumPfProfit(start_time,endTime);
		Double merchantr_cash = merchantCashService.querySumCash(start_time,endTime);
		int clearDate = Integer.valueOf(DateUtil.format(new Date(start_time * 1000), DateUtil.PATTERN_yyyyMMdd));
		Double chanagePrice = ArithUtil.sub(ArithUtil.adds(pay_price,repayment_a,repayment_b),merchantr_cash);
		balance = ArithUtil.add(balance,chanagePrice);

		FundsPfModel pfModel = new FundsPfModel();
		pfModel.setPay_price(pay_price);
		pfModel.setRepayment_a(repayment_a);
		pfModel.setRepayment_b(repayment_b);
		pfModel.setProfit_b(profit_b);
		pfModel.setMerchantr_cash(merchantr_cash);
        pfModel.setBalance(balance);
		pfModel.setClear_date(clearDate);
		pfModel.setCreate_time(currtimes);
         FundsPfModel fundsPfModel = fundsPfDao.getModelOne(FrameUtil.newHashMap("clear_date",clearDate),true);
		if(fundsPfModel!=null){
			fundsPfDao.deleteById(fundsPfModel.getId(),true);
		}
		fundsPfDao.insertModel(pfModel,true);
		return balance;
	}


	@Resource
	private IFundsPfDao fundsPfDao;

	@Resource
	private IRechargeOrderService rechargeOrderService;

	@Resource
	private IRepaymentClearService repaymentClearService;

	@Resource
	private IRepaymentDataService repaymentDataService;

	@Resource
	private IMerchantCashService merchantCashService;

	@Override
	public IBaseDao getBaseDao() {
		return this.fundsPfDao;
	}

}
